package com.twp.auth.rest.api.manage;

import com.twp.auth.domain.authority.Menu;
import com.twp.auth.service.manage.MenuService;
import com.twp.common.component.process.exception.combo.IComboExecptionForTFunc;
import com.twp.common.tuple.ResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Slf4j
@RestController
@RequestMapping(value = "/api/oauth/menu",produces = "application/json; charset=utf-8")
public class MenuController {

    @Autowired
    IComboExecptionForTFunc iComboExecptionForTFunc;

    @Autowired
    MenuService menuService;

    @GetMapping("")
    ResultDTO find(Menu menu, @PageableDefault Pageable pageable,@RequestParam(defaultValue = "false")boolean needsChildren){
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().data("菜单获取成功").build(),
                it-> {
                   it.setData(menuService.find(menu,pageable,needsChildren));
                },
                (e,it)->{
                    it.setMessage("菜单获取失败");
                    it.setIsSuccess(false);
                }
        );
    }

    @GetMapping("/treeMenu")
    ResultDTO find(Menu menu){
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().message("树菜单获取成功").build(),
                it-> {
                    it.setData(menuService.find(menu));
                },
                (e,it)->{
                    it.setMessage("树菜单获取失败");
                    it.setIsSuccess(false);
                }
        );
    }

    @DeleteMapping("/{id}")
    ResultDTO deleteById(@PathVariable String id){
        return iComboExecptionForTFunc
                .executeFlow(
                        ResultDTO.builder().message("删除菜单成功").build(),
                        it->menuService.deleteById(Integer.valueOf(id)),
                        (e,it)->{
                            it.setIsSuccess(false);
                            it.setMessage("删除菜单失败");
                        }
                        );
    }

    @PostMapping("")
    ResultDTO action(@RequestBody Menu menu){
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().message("菜单操作成功").build(),
                it->it.setData(new HashMap(){{put("id",menuService.saveOrUpdate(menu));}}),
                (e,it)->{
                    it.setIsSuccess(false);
                    it.setMessage("操作菜单失败");
                }
        );
    }

    @GetMapping("loadTreeData")
    ResultDTO loadTreeData(String roleId){
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().message("树菜单获取成功").build(),
                it-> {
                    it.setData(menuService.loadTreeData(Integer.parseInt(roleId)));
                },
                (e,it)->{
                    it.setMessage("树菜单获取失败");
                    it.setIsSuccess(false);
                }
        );
    }
}
